package comunicacao;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;

import threads.ReceptorDados;

import mensagens.Mensagem;


public class Comunicacao implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1022987355543646907L;
	private int porta;
	private ReceptorDados receptor;
	 
	public Comunicacao(int porta, ReceptorDados receptor){
		this.porta = porta;
		this.receptor = receptor;
		receptor.start();
	}
	
	public int getPorta() {
		return porta;
	}
	public void setPorta(int porta) {
		this.porta = porta;
	}
	
	public void enviarMensagem(Mensagem mensagem, String ipDestino) throws UnknownHostException, IOException{

		Socket socket = new Socket(ipDestino, porta);
		ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());

		output.writeObject(mensagem);
		socket.close();
		
	}

	public ReceptorDados getReceptor() {
		return receptor;
	}

	public void setReceptor(ReceptorDados receptor) {
		this.receptor = receptor;
	}
	
	
	@SuppressWarnings("deprecation")
	public void close() throws IOException{
		receptor.stop();
		receptor.getSocket().close();		
	}
	
	
	
}
